package com.gljx.web.service.impl;

import com.gljx.web.Entity.JournalEntry.RecruitStudentJournal;
import com.gljx.web.dao.RecruitStudentJournalMapper;
import com.gljx.web.service.RecruitStudentJournalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class RecruitStudentJournalServiceImpl implements RecruitStudentJournalService {
	
	@Autowired
	private RecruitStudentJournalMapper recruitStudentJournalMapper;
	
	@Override
	public List<RecruitStudentJournal> selRecruitStudentJournal(String companyId, String beginTime,
			String endTime,String person,String deparment) {
		// TODO Auto-generated method stub
		List<RecruitStudentJournal> recruitStudentJournalList=new ArrayList<RecruitStudentJournal>();
		//先获取本月(不含未交费,特招 特批)和已交小费 转班人数
		List<RecruitStudentJournal> recruitStudentJournalList_nowmonth=null;
		// 已交小费 人数 和之前入学本月交费人数总和
		List<RecruitStudentJournal> recruitStudentJournalList_agomonth=recruitStudentJournalMapper.selRecruitStudentJournalAgoMonth(companyId, beginTime, endTime);
		//特批和未交费用 特招人数
		List<RecruitStudentJournal> recruitStudentJournalList_notPayAndParticular=recruitStudentJournalMapper.selRecruitStudentJournalnotPayAndParticularAndDropOut(companyId, beginTime, endTime);
		recruitStudentJournalList_nowmonth=recruitStudentJournalMapper.selRecruitStudentJournalNowMonth(companyId, beginTime, endTime,person,deparment);		
		for (RecruitStudentJournal recruitStudentJournal : recruitStudentJournalList_nowmonth) {
				for (int i = 0; i < recruitStudentJournalList_agomonth.size(); i++) {
					//去已交小费 人数 和之前入学本月交费人数总和找推荐人和班级对应的数据
					if(recruitStudentJournal.getPersonName() == null || recruitStudentJournal.getPersonName() == null){
						recruitStudentJournal.setPersonName("");						
					}
					if(recruitStudentJournalList_agomonth.get(i) == null){
						continue;						
					}
					if(recruitStudentJournal.getPersonName().equals(recruitStudentJournalList_agomonth.get(i).getPersonName())
							&& recruitStudentJournal.getClassName().equals(recruitStudentJournalList_agomonth.get(i).getClassName())){
						if(recruitStudentJournal.getPayedCount() == null){
							recruitStudentJournal.setPayedCount(0);
						}
						if(recruitStudentJournalList_agomonth.get(i).getAgoMonthCount() == null){
							recruitStudentJournalList_agomonth.get(i).setAgoMonthCount(0);
						}
						if(recruitStudentJournal.getPayedCount()  < recruitStudentJournalList_agomonth.get(i).getAgoMonthCount()){
							recruitStudentJournal.setAgoMonthCount(recruitStudentJournalList_agomonth.get(i).getAgoMonthCount() - recruitStudentJournal.getPayedCount());
						}
					}
				}
				
				for (int i = 0; i < recruitStudentJournalList_notPayAndParticular.size(); i++) {
					//把特批、和未交费用、退学 特招 人数拼接进去
					if(recruitStudentJournal.getPersonName().equals(recruitStudentJournalList_notPayAndParticular.get(i).getPersonName())
							&& recruitStudentJournal.getClassName().equals(recruitStudentJournalList_notPayAndParticular.get(i).getClassName())){
						if(recruitStudentJournalList_notPayAndParticular.get(i).getNotPayCount() == 0){
							recruitStudentJournal.setNotPayCount(null);
						}else{
							//未交费人数
							recruitStudentJournal.setNotPayCount(recruitStudentJournalList_notPayAndParticular.get(i).getNotPayCount());
						}
						//特招
						recruitStudentJournal.setParticularCount1(recruitStudentJournalList_notPayAndParticular.get(i).getParticularCount1());
						//特批
						recruitStudentJournal.setParticularCount2(recruitStudentJournalList_notPayAndParticular.get(i).getParticularCount2());
						//退学
						recruitStudentJournal.setDropOutCount(recruitStudentJournalList_notPayAndParticular.get(i).getDropOutCount());
					}
				}			
				recruitStudentJournalList.add(recruitStudentJournal);
			/*System.out.println(" 部门: "+recruitStudentJournal.getDepartmentName()+" 人员: "+recruitStudentJournal.getPersonName()
			+" 班级: "+recruitStudentJournal.getClassName()+" 本月: "+recruitStudentJournal.getNowMonthCount()+" 以前入学本月交费: "+recruitStudentJournal.getAgoMonthCount()+" 已交小费: "+recruitStudentJournal.getPayedCount()
			+" 转班: "+recruitStudentJournal.getChangeClassCount()+" 未交费人数: "+recruitStudentJournal.getNotPayCount()+" 特批: "+recruitStudentJournal.getParticularCount2()+" 退学:"+recruitStudentJournal.getDropOutCount());*/
		} 
		return recruitStudentJournalList;
	}

}
